package proxy;

import java.lang.reflect.Proxy;
import boot.RpcApplication;
public class ProxyServiceFactory {
    /**
     * 根据服务字节码获取代理对象
     * @param serviceClass
     * @return
     * @param <T>
     */
    public static <T> T getProxy(Class<T> serviceClass){
        //开启mock后,返回mock对象
        if(RpcApplication.getRpcConfig().getMock())return getMockProxy(serviceClass);
        //否则
        return (T) Proxy.newProxyInstance(serviceClass.getClassLoader(),
                                        new Class[]{serviceClass},
                                        new ServiceProxy());
    }

    /**
     * 获取Mock代理对象(对象实现的接口是serviceClass)
     * @param serviceClass
     * @return
     * @param <T>
     */
    public static <T> T getMockProxy(Class<T> serviceClass){
        return (T) Proxy.newProxyInstance(serviceClass.getClassLoader(),
                new Class[]{serviceClass},
                new MockServiceProxy());
    }
}
